<?php
class ModelFaCustomer extends Model {
	public function addCustomer($data) {
      	$this->db->query("INSERT INTO " . DB_PREFIX . "fa_customer SET short_name = '". $this->db->escape($data['short_name']) . "', name = '" . $this->db->escape($data['name']) . "', address = '" . $this->db->escape($data['address']) . "', tax_code = '". $this->db->escape($data['tax_code']) ."', sort_order = '" . (int)$data['sort_order'] . "'");
		
		$customer_id = $this->db->getLastId();

		//add address
		if (isset($data['address_secons'])) {
			foreach ($data['address_secons'] as $address) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "fa_customer_address SET customer_id = '" . $customer_id . "', address = '" . (int)$address['address'] . "', name = '" . $address['name'] . "'");
			}
		}

		//$this->db->query("INSERT INTO " . DB_PREFIX . "fa_customer_address SET address = '". $this->db->escape($data['customer_address']) . "', customer_id = '" . (int)$customer_id . "'");
		
		$this->cache->delete('fa_customer');
	}
	
	public function editCustomer($customer_id, $data) {
      	$this->db->query("UPDATE " . DB_PREFIX . "fa_customer SET short_name = '". $this->db->escape($data['short_name']) ."', name = '" . $this->db->escape($data['name']) . "', address = '" . $this->db->escape($data['address']) . "', tax_code = '". $this->db->escape($data['tax_code']). "', sort_order = '" . (int)$data['sort_order'] . "' WHERE customer_id = '" . (int)$customer_id . "'");

      	//add address
		if (isset($data['address_secons'])) {
			$this->db->query("DELETE FROM " . DB_PREFIX . "fa_customer_address WHERE customer_id = '" . (int)$customer_id . "'");
			foreach ($data['address_secons'] as $address) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "fa_customer_address SET customer_id = '" . $customer_id . "', address = '" . $this->db->escape($address['address_customer']) . "', name = '" . $address['name'] . "'");
			}
		}else{
			$this->db->query("DELETE FROM " . DB_PREFIX . "fa_customer_address WHERE customer_id = '" . (int)$customer_id . "'");
		}

		//$this->db->query("UPDATE " . DB_PREFIX . "fa_customer_address SET address = '". $this->db->escape($data['customer_address']) . "' WHERE customer_id = '" . (int)$customer_id . "'");
		
		$this->cache->delete('fa_customer');
	}
	
	public function deleteCustomer($customer_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "fa_customer WHERE customer_id = '" . (int)$customer_id . "'");

		//delete address
		$this->db->query("DELETE FROM " . DB_PREFIX . "fa_customer_address WHERE customer_id = '" . (int)$customer_id . "'");
		//$this->db->query("DELETE FROM " . DB_PREFIX . "fa_customer_address WHERE customer_id = '" . (int)$customer_id . "'");		
			
		$this->cache->delete('fa_customer');
	}	
	
	public function getCustomer($customer_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "fa_customer WHERE customer_id = '" . (int)$customer_id . "'");
		
		return $query->row;
	}
	
	public function getCustomers($data = array()) {
		$sql = "SELECT * FROM " . DB_PREFIX . "fa_customer";

		if (!empty($data['filter_name'])) {
			$sql .= " WHERE name LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
		}

		if (!empty($data['filter_short_name'])) {
			$sql .= " WHERE short_name LIKE '%" . $this->db->escape($data['filter_short_name']) . "%'";
		}
		
		$sort_data = array(
			'name',
			'sort_order'
		);	
		
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY name";	
		}
		
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}					

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
		
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}				
		
		$query = $this->db->query($sql);
	
		return $query->rows;
	}

	//get address
	public function getAddress($customer_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "fa_customer_address WHERE customer_id = '" . (int)$customer_id . "'");
		return $query->rows;
	}
	
	public function getTotalCustomers() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "fa_customer");
		
		return $query->row['total'];
	}	
	
}
?>